package com.bzkj.basicinfo.service.outbound.impl;

import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsList;
import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsDetail;
import com.bzkj.basicinfo.mapper.PdaWarehouseOutBillsMapper;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.utils.PageUtils;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.basicinfo.service.outbound.WmsOutboundOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * WMS出库单Service实现
 *
 * @author system
 * @date 2025-10-22
 */
@Service
public class WmsOutboundOrderServiceImpl implements WmsOutboundOrderService {

    @Autowired
    private PdaWarehouseOutBillsMapper pdaWarehouseOutBillsMapper;

    @Override
    public TableDataInfo getOutboundOrderList(PdaWarehouseOutBillsList entity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PdaWarehouseOutBillsList> list = pdaWarehouseOutBillsMapper.selectPdaWarehouseOutBillsList(entity);
        PageInfo<PdaWarehouseOutBillsList> pageInfo = new PageInfo<>(list);
        return PageUtils.getTableDataInfo(pageInfo);
    }

    @Override
    public PdaWarehouseOutBillsList getOutboundOrderDetail(String billsId) {
        return pdaWarehouseOutBillsMapper.selectPdaWarehouseOutBillsByBillsID(billsId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createOutboundOrder(PdaWarehouseOutBillsList entity) {
        // 生成出库单号：OUT + YYYYMMDDHHMMSS
        String billsId = "OUT" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        entity.setBillsId(billsId);
        entity.setBillsState("0"); // 待拣货
        entity.setCheckDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        entity.setUserId(SecurityUtils.getUserId().toString());

        // 插入主表
        int result = pdaWarehouseOutBillsMapper.insertPdaWarehouseOutBillsList(entity);

        // 插入明细（如果有）
        if (entity.getOutDetailList() != null && !entity.getOutDetailList().isEmpty()) {
            for (PdaWarehouseOutBillsDetail detail : entity.getOutDetailList()) {
                detail.setBillsId(billsId);
                detail.setStoId(entity.getStoId());
                detail.setUserId(entity.getUserId());
            }
            pdaWarehouseOutBillsMapper.batchInsertPdaWarehouseOutBillsDetail(entity.getOutDetailList());
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOutboundOrder(PdaWarehouseOutBillsList entity) {
        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOutboundOrder(String billsId) {
        // 删除明细
        pdaWarehouseOutBillsMapper.deletePdaWarehouseOutBillsDetailByBillsID(billsId);
        // 删除主表（逻辑删除，更新状态为3）
        return pdaWarehouseOutBillsMapper.deletePdaWarehouseOutBillsByBillsID(billsId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkConfirm(String billsId, String checker) {
        PdaWarehouseOutBillsList entity = new PdaWarehouseOutBillsList();
        entity.setBillsId(billsId);
        entity.setBillsState("3"); // 复核完成
        entity.setReviewUserId(SecurityUtils.getUserId().toString());
        entity.setReviewUserName(checker);
        entity.setReviewTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int shipConfirm(String billsId, String shipper) {
        PdaWarehouseOutBillsList entity = new PdaWarehouseOutBillsList();
        entity.setBillsId(billsId);
        entity.setBillsState("4"); // 已发货
        entity.setOutUserId(SecurityUtils.getUserId().toString());
        entity.setOutUserName(shipper);
        entity.setOutTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelOutboundOrder(String billsId, String reason) {
        PdaWarehouseOutBillsList entity = new PdaWarehouseOutBillsList();
        entity.setBillsId(billsId);
        entity.setBillsState("9"); // 异常/已取消
        entity.setRemarks1(reason);
        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(entity);
    }
}
